package com.haiberg.framework.dao;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.haiberg.framework.om.Person;

@Repository("genericsClassDAO")
public class GenericsClassDAO<T>  extends HibernateDaoSupport{
	private static final Logger logger = Logger.getLogger(GenericsClassDAO.class);
	public int saveRecord(T aT)
	{
		logger.debug("-----------save record start ...");
		int res = 0;
		try {
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.save(aT);
			session.flush();
			session.evict(aT);
			res = 1;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("----------save error!"+e.getMessage());
		}
		logger.debug("-----------save record finish.");
		return res;
	}
	public T findRecord(Class<T> entityClass, Serializable id)
	{
		logger.debug("-----------find record start ...");
		try {
			return getHibernateTemplate().get(entityClass, id);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("----------find error!"+e.getMessage());
		}
		logger.debug("-----------find record finish.");
		return null;
	}
	public int deleteRecord(T aT)
	{
		logger.debug("-----------delete record start ...");
		int res = 0;
		try {
			getHibernateTemplate().delete(aT);
			res = 1;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("----------delete error!"+e.getMessage());
		}
		logger.debug("-----------delete record finish.");
		return res;
	}
	public int updateRecord(T aT)
	{
		logger.debug("-----------update record start ...");
		int res = 0;
		try {
			getHibernateTemplate().update(aT);
			res = 1;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("----------update error!"+e.getMessage());
		}
		logger.debug("-----------update record finish.");
		return res;
	}
	public List<T> findAll(String hql)
	{
		logger.debug("-----------find record start ...");
		try {
			return (List<T>) getHibernateTemplate().find(hql);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("----------find error!"+e.getMessage());
		}
		logger.debug("-----------find record finish.");
		return null;
	}
	public List<T> findByCriteira(Map<String, String> criteria, String tableName)
	{
		if(criteria != null)
		{
			try {
				StringBuffer sb = new StringBuffer();
				for (Iterator<String> it = criteria.keySet().iterator(); it
						.hasNext();) {
					String key = it.next();
					String val = criteria.get(key);
					sb.append("and lower(p." + key + ") like lower('%" + val
							+ "%')");

				}
				String sql = "select {p.*} from " + tableName + " where 1=1 "
						+ sb.toString() + " order by p.id";
				Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
				SQLQuery query = session.createSQLQuery(sql);
				query.addEntity("p", Person.class);
				return query.list();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
